import * as React from 'react';
import {ConfigConsumer, ConfigContext, withConfigConsumer} from 'antd/es/config-provider/context';
import {omit} from "lodash-es";
import globalHelper from '../../utils/globalHelper'

export const GlobalContext = ConfigContext;
export const GlobalConsumer = ConfigConsumer;
export const withGlobalConsumer = withConfigConsumer;

class GlobalProvider extends React.Component {

    constructor(props) {
        super(props);
        globalHelper.setValues({...omit(props, ['children', 'contextValue'])});
    }

    renderProvider = (context) => {
        const {contextValue = {}} = this.props;

        return (
            <ConfigContext.Provider value={{
                ...context,
                ...contextValue
            }}>
                {this.props.children}
            </ConfigContext.Provider>
        );
    };

    render() {
        return (
            <ConfigConsumer>
                {this.renderProvider}
            </ConfigConsumer>
        );
    }
}

export default GlobalProvider;